<template>
    <div>
        <el-container>
            <el-main>
                <el-form
                        :model="dataForm"
                        :inline="true"
                        :rules="dataRule"
                        label-width="130px">
                    <el-form-item label="企业名称" prop="companyName">
                        <el-col :span="24">
                            <el-input v-model="dataForm.companyName" placeholder="企业名称"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="企业性质" prop="companyPropertyId">
                        <template>
                            <el-col :span="23">
                                <el-select v-model="dataForm.companyPropertyId" placeholder="请选择">
                                    <el-option
                                            v-for="item in selectOption"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-col>
                        </template>

                    </el-form-item>
                    <el-form-item label="注册资金" prop="registerMany" label-width="115px">
                        <el-col :span="24">
                            <el-input v-model="dataForm.registerMany" placeholder="注册资金"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="企业官网" prop="companyUrl">
                        <el-col :span="24">
                            <el-input v-model="dataForm.companyUrl" placeholder="企业官网"></el-input>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="所属行业">
                        <el-col :span="23">
                            <el-cascader
                                    v-model="dataForm.industryArrOptions"
                                    :options="industryOptions"
                                    @change="handleChangeIndustory">
                            </el-cascader>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="企业地址" label-width="115px">
                        <el-col :span="22">
                            <el-cascader
                                    v-model="dataForm.companyAddressOptions"
                                    :options="companyAddressOptions"
                                    @change="handleChangeAddress">
                            </el-cascader>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="联系人" prop="companyContact">
                        <el-col :span="24">
                            <el-input v-model="dataForm.companyContact" placeholder="联系人"></el-input>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="联系电话" prop="companyPhone">
                        <el-col :span="24">
                            <el-input v-model="dataForm.companyPhone" placeholder="联系电话"></el-input>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="联系微信" prop="companyWechat">
                        <el-col :span="24">
                            <el-input v-model="dataForm.companyWechat" placeholder="联系微信"></el-input>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="联系固话" prop="companyFixPhone">
                        <el-col :span="24">
                            <el-input v-model="dataForm.companyFixPhone" placeholder="联系固话"></el-input>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="联系邮箱" prop="companyEmail" label-width="130px">
                        <el-col :span="24">
                            <el-input v-model="dataForm.companyEmail" placeholder="联系邮箱"></el-input>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="联系QQ" prop="companyQq">
                        <el-col :span="24">
                            <el-input v-model="dataForm.companyQq" placeholder="联系QQ"></el-input>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="详细地址" prop="detailPosition">
                        <el-col :span="24">
                            <el-input v-model="dataForm.detailPosition" placeholder="详细地址"></el-input>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="企业福利" prop="detailPosition">
                        <el-col :span="24">
                            <el-select
                                    style="width: 300px"
                                    v-model="dataForm.companyWalfare"
                                    clearable
                                    multiple
                                    placeholder="请选择">
                                <el-option
                                        v-for="item in walfareOption"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="企业介绍" prop="companyIntrl">
                        <el-col :span="24">
                            <el-input
                                    style="width: 500px"
                                    type="textarea"
                                    maxlength="100"
                                    :rows = "2"
                                    show-word-limit v-model="dataForm.companyIntrl"
                                    placeholder="企业介绍">
                            </el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item >
                        <el-button style="width: 150px;margin-left: 100px" type="primary" @click="dataFormSubmit()">保存</el-button>
<!--                      <el-button style="width: 150px" @click="clearDataForm">信息重置</el-button>-->
                    </el-form-item>
                </el-form>
            </el-main>


            <div>
                <span v-if="dataForm.isPass === 1" style="color: rgba(103,194,58,0.5)">企业资质审核通过</span>
                <span v-else-if="dataForm.isPass === 0"  style="color: rgba(245,108,108,0.5)">企业资质未审核通过</span>
            </div>
        </el-container>
    </div>
</template>

<script>
    import commonApi from "@/api/commonApi";
    import compansApi from "@/api/companys/compansApi";
    export default {
        inject:['reload'],
        name: "company-create-info",
        data() {
            return {
                dataForm: {
                    id: '' || undefined,
                    companyWalfare:[],
                    companyName: '',
                    registerMany: '',
                    companyUrl: '',
                    companyContact: '',
                    companyPhone: '',
                    companyWechat: '',
                    companyFixPhone: '',
                    companyEmail: '',
                    companyQq: '',
                    provincesId: '',
                    cityesId: '',
                    areasId: '',
                    tradeId: '',
                    classifyId: '',
                    jobTypeId: '',
                    industryArrOptions:[],
                    companyAddressOptions:[],
                    detailPosition: '',
                    companyIntrl: '',
                    companyPropertyId: '',
                    isPass:-1,
                    companyApproveStatus:''
                },
                userId:'',                              //当前用户的Id
                dataRule: {
                    companyName: [
                        { required: true, message: '企业名称不能为空', trigger: 'blur' }
                    ],
                    registerMany: [
                        { required: true, message: '注册资金不能为空', trigger: 'blur' }
                    ],
                    companyUrl: [
                        { required: true, message: '企业官网不能为空', trigger: 'blur' }
                    ],
                    companyContact: [
                        { required: true, message: '联系人不能为空', trigger: 'blur' }
                    ],
                    companyPhone: [
                        { required: true, message: '联系电话不能为空', trigger: 'blur' }
                    ],
                    companyWechat: [
                        { required: true, message: '联系微信不能为空', trigger: 'blur' }
                    ],
                    companyFixPhone: [
                        { required: true, message: '联系固话不能为空', trigger: 'blur' }
                    ],
                    companyEmail: [
                        { required: true, message: '联系邮箱不能为空', trigger: 'blur' }
                    ],
                    companyQq: [
                        { required: true, message: '联系QQ不能为空', trigger: 'blur' }
                    ],
                    detailPosition: [
                        { required: true, message: '详细地址不能为空', trigger: 'blur' }
                    ],
                    companyPropertyId: [
                        { required: true, message: '企业性质不能为空', trigger: 'blur' }
                    ]
                },
                walfareOption:[],
                selectOption:[],
                industryOptions:[],
                companyAddressOptions:[],
            }
        },
        created() {},
        mounted() {
            this.loadingIndustory()
            this.loadingCompanyAddress()
            this.loadingCompanyProperty()
            this.loadingCompanyWalfare()
            this.init()
            this.loadingCompanyAddressUser()
            this.loadingCompanyIndustry()
            this.loadingCompanyWalfareUser()
        },
        methods:{
            //加载行业选项
            loadingIndustory(){
                const userId = JSON.parse(localStorage.getItem("user")).userId;
                commonApi.loadingIndustoryByUser(userId)
                .then(data => {
                    if (data && data.data.code=== 0){
                        this.industryOptions = data.data.data
                    }
                })
                .catch(error => {
                    Promise.reject(error)
                })
            },
            //加载企业地址选项
            loadingCompanyAddress(){
                commonApi.loadingCompanyAddress()
                .then(data =>{
                    if (data && data.data.code === 0){
                        this.companyAddressOptions = data.data.data
                    }
                })
                .catch(error =>{
                    Promise.reject(error)
                })
            },
            //加载企业的性质
            loadingCompanyProperty(){
                commonApi.loadingCompanyProperty()
                .then(data => {
                    if (data && data.data.code === 0){
                        this.selectOption = data.data.data
                    }
                })
                .catch(error => {
                    Promise.reject(error)
                })
            },
            //加载企业的福利列表
            loadingCompanyWalfare(){
                commonApi.loadingWelfare()
                .then(data => {
                    if (data && data.data.code === 0){
                        this.walfareOption = data.data.data
                    }
                })
                .catch(error => {
                    Promise.reject(error)
                })
            },
            //根据用户的Id加载地址
            loadingCompanyAddressUser(){
                const userId = JSON.parse(localStorage.getItem("user")).userId
                commonApi.loadingCompanyAddressUser(userId)
                .then(data => {
                    if (data.data.code === 0){
                        this.dataForm.companyAddressOptions = data.data.data
                    }
                })
                .catch(error =>{
                    Promise.reject(error)
                })
            },
            //根据用户的Id加载行业
            loadingCompanyIndustry(){
                const userId = JSON.parse(localStorage.getItem("user")).userId
                commonApi.loadingCompanyIndustry(userId)
                .then(data => {
                    this.dataForm.industryArrOptions = data.data.data
                })
                .catch(error => {
                    Promise.reject(error)
                })
            },
            //根据用户的Id加载福利
            loadingCompanyWalfareUser(){
                const userId = JSON.parse(localStorage.getItem("user")).userId
                commonApi.loadingCompanyWalfareUser(userId)
                .then(data => {
                    if (data.data.code === 0){
                        this.dataForm.companyWalfare = data.data.data
                    }
                })
                .catch(error => {
                    Promise.reject(error)
                })
            },


            init(){
                const userId = JSON.parse(localStorage.getItem('user')).userId
                compansApi.init(userId)
                .then(data => {
                    this.dataForm.companyAddressOptions
                    if (data.data.code === 0){
                        this.dataForm.id =data.data.company.id
                        this.dataForm.companyName = data.data.company.companyName
                        this.dataForm.registerMany = data.data.company.registerMany
                        this.dataForm.companyUrl = data.data.company.companyUrl
                        this.dataForm.companyContact = data.data.company.companyContact
                        this.dataForm.companyPhone = data.data.company.companyPhone
                        this.dataForm.companyWechat = data.data.company.companyWechat
                        this.dataForm.companyFixPhone = data.data.company.companyFixPhone
                        this.dataForm.companyEmail = data.data.company.companyEmail
                        this.dataForm.companyQq = data.data.company.companyQq
                        this.dataForm.provincesId = data.data.company.provincesId
                        this.dataForm.cityesId = data.data.company.cityesId
                        this.dataForm.areasId = data.data.company.areasId
                        this.dataForm.detailPosition = data.data.company.detailPosition
                        this.dataForm.companyIntrl = data.data.company.companyIntrl
                        this.dataForm.tradeId = data.data.company.tradeId
                        this.dataForm.classifyId = data.data.company.classifyId
                        this.dataForm.jobTypeId = data.data.company.jobTypeId
                        this.dataForm.companyPropertyId = data.data.company.companyPropertyId.toString()
                        this.dataForm.companyWalfare = data.data.company.walfareOption
                        this.dataForm.isPass = data.data.company.isPass
                    }
                })
                .catch(error => {
                    Promise.reject(error)
                })
            },

            //dataFormSubmit
            dataFormSubmit(){

/*                console.log("this.dataForm.industryOptions.length",this.dataForm.industryArrOptions)
                console.log("this.dataForm.companyAddressOptions.length ==>",this.dataForm.companyAddressOptions)
                console.log("this.dataForm.companyWalfare.length ===>>",this.dataForm.companyWalfare.length)
                console.log("this.dataForm.companyAddressOptions.length ==>",this.dataForm.companyAddressOptions.length)
                console.log("this.dataForm.industryOptions.length",this.dataForm.industryArrOptions.length)*/


                if (this.dataForm.companyName == '' || this.dataForm.companyName == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写企业的名称'
                    })
                    return
                }

                if (this.dataForm.companyPropertyId == '' || this.dataForm.companyPropertyId == undefined){
                    this.$message({
                        type:'warning',
                        message:'请选择企业的性质'
                    })
                    return
                }

                if (this.dataForm.registerMany == '' || this.dataForm.registerMany == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写注册资金'
                    })
                    return;
                }
                if (this.dataForm.registerMany<=0){
                    this.$message({
                        type:'warning',
                        message:'输入的数据不合法'
                    })
                    return;
                }

                if (this.dataForm.companyUrl == '' || this.dataForm.companyUrl == undefined){
                    this.$message({
                        type:'warning',
                        message:'请输入企业的官网信息'
                    })
                    return;
                }

                /*官网正则*/
                debugger
                var urlRegex = '(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?'

                console.log("urlRegex ===> ",this.dataForm.companyUrl.match(urlRegex))

                if (this.dataForm.companyUrl.match(urlRegex) == null){
                    this.$message({
                        type:'warning',
                        message:'官网信息格式不正确'
                    })
                    return
                }


                if (this.dataForm.industryArrOptions == undefined){
                    this.$message({
                        type:'warning',
                        message:'请选择行业'
                    })
                    return;
                }

                if (this.dataForm.industryArrOptions == undefined){
                    this.$message({
                        type:'warning',
                        message:'请选择企业的地址'
                    })
                    return;
                }

                if (this.dataForm.companyContact == '' || this.dataForm.companyContact == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写联系人'
                    })
                    return;
                }

                if (this.dataForm.companyPhone == '' || this.dataForm.companyPhone == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写联系电话'
                    })
                    return;
                }

                if (this.dataForm.companyWechat == '' || this.dataForm.companyWechat == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写联系微信号'
                    })
                    return;
                }

                if (this.dataForm.companyFixPhone == '' || this.dataForm.companyFixPhone == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写联系固话'
                    })
                    return;
                }

                if (this.dataForm.companyEmail == '' || this.dataForm.companyEmail == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写联系邮箱'
                    })
                    return;
                }

                if (this.dataForm.companyQq == '' || this.dataForm.companyQq == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写联系qq'
                    })
                    return;
                }

                if (this.dataForm.detailPosition == '' || this.dataForm.detailPosition == undefined){
                    this.$message({
                        type:'warning',
                        message:'请填写详细地址信息'
                    })
                    return;
                }





















                this.userId = JSON.parse(localStorage.getItem('user')).userId
                let dataObj = {
                    'companyWalfare':this.dataForm.companyWalfare,
                    'id': this.dataForm.id || undefined,
                    'companyName': this.dataForm.companyName,
                    'registerMany': this.dataForm.registerMany,
                    'companyUrl': this.dataForm.companyUrl,
                    'companyContact': this.dataForm.companyContact,
                    'companyPhone': this.dataForm.companyPhone,
                    'companyWechat': this.dataForm.companyWechat,
                    'companyFixPhone': this.dataForm.companyFixPhone,
                    'companyEmail': this.dataForm.companyEmail,
                    'companyQq': this.dataForm.companyQq,
                    'provincesId': this.dataForm.provincesId,
                    'cityesId': this.dataForm.cityesId,
                    'areasId': this.dataForm.areasId,
                    'detailPosition': this.dataForm.detailPosition,
                    'companyIntrl': this.dataForm.companyIntrl,
                    'tradeId': this.dataForm.tradeId,
                    'classifyId': this.dataForm.classifyId,
                    'jobTypeId': this.dataForm.jobTypeId,
                    'companyPropertyId': this.dataForm.companyPropertyId,
                    'createUserId':this.userId
                }
                compansApi.createCompany(dataObj)
                .then(response => {



                    if (response.data.code == 0){

                        if (this.dataForm.id == undefined){
                            this.$message({
                                type:'success',
                                message:'创建企业信息成功'
                            })
                        }else{
                            this.$message({
                                type:'success',
                                message:'企业信息修改成功'
                            })
                        }
                        this.reload()
                    }
                })
                .catch(error => {
                    Promise.reject(error)
                })
            },
            handleChangeIndustory(value){
                //console.log("行业的value ===》",value)
                this.dataForm.tradeId = value[0]
                this.dataForm.classifyId = value[1]
                this.dataForm.jobTypeId = value[2]
            },
            handleChangeAddress(value){
                //console.log("地址的value ===》",value)
                this.dataForm.provincesId = value[0]
                this.dataForm.cityesId = value[1]
                this.dataForm.areasId = value[2]
            }
        }
    }
</script>

<style scoped>
    .el-col {
        border-radius: 4px;
    }
    .bg-purple-dark {
        background: #FCFCFC;
    }
    .bg-purple {
        background: #d3dce6;
    }
    .bg-purple-light {
        background: #e5e9f2;
    }
    .grid-content {
        border-radius: 4px;
        min-height: 36px;
    }
    .row-bg {
        padding: 10px 0;
        background-color: #f9fafc;
    }
</style>
